const tree=require('./tree.json');

const mapLimit=(children,limit,cb)=>{
    let ret=[],ing=0,count=0;

/*    function iife(idx) {
        for(idx;idx<children.length;idx++){
            if(ing>=limit)break;
            ret[idx]=cb(children[idx],idx,children);
            ing++;
            count++;
        }
        if(count==children.length)return ret;
    }
    0==count&&iife(count);*/

    (function iife(idx) {

    })(0);
    for(let i=0;i<children.length;i++){
        if(ing>=limit)break;
        ret[i]=()=>cb(children[i],i,children);
        ing++;
        count++;
    }


    return ret.map(fn=>fn());
};
const create=(tree,id)=>{
    return {
        id,
        //children:tree.children.map(child=>create(child,child.id))
        children:mapLimit(tree.children,2,(child,i,children)=>create(child,child.id))
    }
};
let newTree=create(tree,'root');

console.log(newTree);
console.log(JSON.stringify(newTree,null,2));

